//测试地址
//const baseUrl = 'http://f6288625.natappfree.cc/'
//const baseUrl = 'http://t8ced87b.natappfree.cc/'

//正式

//const baseUrl = 'http://1.94.148.142:36310/'
const baseUrl = 'https://www.datiepen.com/'

console.log(baseUrl)
const http = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		//console.log(baseUrl)
		//const wxOpenData = uni.getStorageSync("wxOpenData");
		/* var wxOpenData= uni.getStorageSync("wxOpenData")
		if(wxOpenData.loginInfo){
			header['token']=wxOpenData.loginInfo.token;
			header['uid']=wxOpenData.loginInfo.uid; 
		} */
		//console.log($store.state.carInfo)
		//header['x-access-token']='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjEyMzQ1NjUiLCJleHAiOjE3NTUwMzI2Mjh9.ct9Pt1W9ZFgCIe7C-khj7T2vJaaKT5zI1-Ps0dsKmBg';
		//header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A'; 
		//header['Content-Type']='application/x-www-form-urlencoded';
		
		var token= uni.getStorageSync("token")
		//token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im9OZVpLMTgyRkZ1TDJvSzY0QWZWRDlKSTZYM3ciLCJleHAiOjE3NTc0NjU1NTV9.H3Ikc2mQzXqWNy-F86gdMI7hVyOX06nXbgQXClpuRBc"
		header['x-access-token']=token
		header['Content-Type']='application/json;charset=utf-8';
		//console.log(header)
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json',         
        }).then((response) => {
            //console.log(response)
           // let [error, res] = response;
		 
			/* if(res.data.code==401||res.data.code==403){
				//  uni.showToast({
				//      title: res.data.msg,
				//  	 icon:'none',
				//      duration: 3000,
				//  });
				// uni.clearStorageSync()
				//  uni.navigateTo({
				// 	url:'../login/login'
				// }) 
			}else if(res.data.code==409){
				 // getToken(url,date,type,header)
			}
			if(res.data.code==1){
				  uni.showToast({
				      title: res.data.msg,
				  	 icon:'none',
				      duration: 4000,
				  });
			} */
			if(url!='jeecg-boot/app/market/bicCoupon/pushActivityCoupon'){
				 if(response.data.code==401){
					 uni.navigateTo({
						url:'../index/login'
					 }) 
				} 
			}
			
            resolve(response.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        }) 
    });
}
// 请求图片流
const httpBinary = (url = '', date = {}, type = 'POST', header = {
}) => {
    return new Promise((resolve, reject) => {
		header={};
		//const wxOpenData = uni.getStorageSync("wxOpenData");
		var wxOpenData=$store.state.wxOpenData
		if(wxOpenData){	
			header['token']=wxOpenData.loginInfo.token;
			header['uid']=wxOpenData.loginInfo.uid; 
		}
		//header['token']='98A9FED8814B457288166BDF623F33D1';
		//header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A'; 
		//header['Content-Type']='application/x-www-form-urlencoded';
		header['Content-Type']='application/json;charset=utf-8';
		//console.log(header)
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json', 
			responseType: "arraybuffer",
        }).then((response) => {
           /* setTimeout(function() {
                uni.hideLoading();
            }, 200); */
            let [error, res] = response;
		 
			if(res.data.code==401||res.data.code==403){
				 uni.showToast({
				     title: res.data.msg,
				 	 icon:'none',
				     duration: 3000,
				 });
				 uni.clearStorageSync()
				 uni.navigateTo({
					url:'../login/login'
				}) 
			}else if(res.data.code==409){
				 // getToken(url,date,type,header)
			}
			
            resolve(res.data);
        }).catch(error => {
			uni.hideLoading();
            let [err, res] = error;
            reject(err)
        }) 
    });
}


function refresh(url,date,type,header){
	uni.request({
	    method: type,
	    url: baseUrl + url,
	    data: date,
	    header: header,
	    dataType: 'json',         
	}).then((response) => {
	   setTimeout(function() {
	        uni.hideLoading();
	    }, 200); 
	    let [error, res] = response;
	  
		if(res.data.code==403){
			uni.showToast({
			     title: res.data.msg,
			 	 icon:'none',
			     duration: 3000,
			 });
			 uni.navigateTo({
				url:'../login/login'
			}) 
		}
	    resolve(res.data);
	}).catch(error => {
	    let [err, res] = error;
	    reject(err)
	})
}

export default {
	http,baseUrl,httpBinary
}